//
//  DetailBookViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-12.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "DetailBookViewController.h"

static ASIHTTPConnection *detailBook_connect;

@interface DetailBookViewController ()

@end

@implementation DetailBookViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc{
    
    if (detailBook_connect != nil) {
        detailBook_connect.connectionDelegate = nil;
        [detailBook_connect release];
        detailBook_connect = nil;
    }
    
    [myArray release];
    [myTableView release];
    
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
     self.title = @"预约详情";
    
    myArray = [[NSMutableArray alloc]init];
    
    [self loadDetailBookData];
    // Do any additional setup after loading the view from its nib.
}

- (void)loadDetailBookData{
    
    NSString *LoginUser_ID = [[NSUserDefaults standardUserDefaults] valueForKey:@"userId"];
    
    NSString *urlString = [NSString stringWithFormat:@"%@/appointmentInfo?LoginUser_ID=%@",UCENTERBASEURL,LoginUser_ID];
	NSURL *url = [NSURL URLWithString:urlString];
    
    if (detailBook_connect != nil) {
        detailBook_connect.connectionDelegate = nil;
        [detailBook_connect release];
        detailBook_connect = nil;
    }
    
    detailBook_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    detailBook_connect.connectionDelegate = self;
    
    [detailBook_connect startAsynchronous];

}

#pragma mark connectino delegate
- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    NSArray *array = [dict objectForKey:@"appointmentinfo"];
    
    if (myArray != nil) {
        [myArray removeAllObjects];
    }
    
    [myArray addObjectsFromArray:array];
    [myTableView reloadData];
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return myArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MyIdentifier = @"creditCell";
    
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] init] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
        [titleLable setTextAlignment:NSTextAlignmentLeft];
        [titleLable setTextColor:[UIColor blackColor]];
        titleLable.backgroundColor = [UIColor clearColor];
        titleLable.tag = 100;
        [cell addSubview:titleLable];
      
        [titleLable release];
        
        UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(10,40 , 290, 20)];
        lable1.tag = 101;
        lable1.backgroundColor = [UIColor clearColor];
        lable1.textColor = [UIColor blackColor];
        [cell addSubview:lable1];
        [lable1 release];
        
        
        UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(10,60 , 250, 20)];
        lable2.tag = 102;
        lable2.backgroundColor = [UIColor clearColor];
        lable2.textColor = [UIColor blackColor];
        [cell addSubview:lable2];
        [lable2 release];
        
    }
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    
    UILabel *lable = (UILabel *)[cell viewWithTag:100];
    lable.text = [dict objectForKey:@"detail_Name"];

    UILabel *lable1 = (UILabel *)[cell viewWithTag:101];
    NSString *lab = [dict objectForKey:@"lab_Name"];
    NSString *doctor = [dict objectForKey:@"doctor_Name"];
    lable1.text = [NSString stringWithFormat:@"%@:%@医生",lab,doctor];
    
    UILabel *lable2 = (UILabel *)[cell viewWithTag:102];
    NSString *duDay = [dict objectForKey:@"duty_Day"];
    lable2.text = [NSString stringWithFormat:@"有效日期：%@",duDay];
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 110;
}


@end
